package com.tbit.uqbike.client.remote.tenxun;

import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
/**
 * 描述这个类是干什么的 todo
 *
 * <p>
 * 1、步骤1 <br> todo
 * 2、步骤2 <br> todo
 * 3、步骤3 todo
 * </p>
 *
 * @author <a href="mailto:chen.xiaoliang@tbit.com.cn">chen</a>
 * @version 1.0 todo
 * @see <a href="http://adsl1.tbit.com.cn:21490/pages/viewpage.action?pageId=82182491">订单号规范</a> todo
 * @since 2025/2/19
 */
public class WechatPayV3Validator {

    public static boolean verifySignature(String wechatpaySignature, String timestamp, String nonce, String body, String publicKeyStr) {
        try {
            // 构造验签字符串
            String signContent = String.format("%s\n%s\n%s\n%s\n%s\n", "POST", "/your/callback/url", timestamp, nonce, body);

            // 解码微信支付签名
            byte[] signatureBytes = Base64.getDecoder().decode(wechatpaySignature);

            // 加载微信支付平台公钥
            byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyStr);
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PublicKey publicKey = keyFactory.generatePublic(keySpec);

            // 使用SHA256-RSA验签
            Signature signature = Signature.getInstance("SHA256withRSA");
            signature.initVerify(publicKey);
            signature.update(signContent.getBytes(StandardCharsets.UTF_8));

            // 验证签名
            return signature.verify(signatureBytes);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void main(String[] args) {
       // 示例数据
        String wechatpaySignature = "WECHATPAY/SIGNTEST/5hYhKtQR0w7TRAlSNLnPgTza0E/+jemJDlfhOjznpOOHI/IE410eDoE+olqyGNxFZwYvd8soC4bCrVkcBBDDHv1KacQDo7umofzntSYK4Dk+pzG05/VTtLBPWRYy42/dbcwCVJ1d37wPb7CCNmN5+V/CxgqN1wc5TvzT7Q/UhKghjiik2cypZIfKB3CNAuM9QIp4u7iy+em6Wj3TNtNLVYcR0xQ1svhLQuLV+STP9EJcY32KbiqxX+cY49lrKEYKVmYwtaJGtmRDQohGH8I6Oh+07LQnsRI21QBbo22uVSjjJZ80GqeV4pVvK9OopvBXVQTFyiV2pT7wQrA==";
        String timestamp = "1739937432";
        String nonce = "K46SpjXFw3PIDBxkQaFtiU4fbmQW7XoJ";
        String body = "{\"id\":\"3bd263c0-efa1-5c09-ba8f-fbd44a46620a\",\"create_time\":\"2025-02-19T11:53:18+08:00\",\"resource_type\":\"encrypt-resource\",\"event_type\":\"PAYSCORE.USER_PAID\",\"summary\":\"微信支付分服务订单支付成功\",\"resource\":{\"original_type\":\"payscore\",\"algorithm\":\"AEAD_AES_256_GCM\",\"ciphertext\":\"JRQKZU0NZiLhGjMUBhcKcFF7MAvxKnCmdTL8oMy0PgoCYuxXMYGEotknWOKOEXQucAV4pNhCuADMqcQGlV1AUL2qF6mRSBQ2ddB6krw9TwJCwoZN4E9vO+YNPFI4me4UumszINv6MYm9+FF797XZbsBhrnsC3Sx+pT1FHg3JT4mjAPs1BFLI/7Q22QGsq6jNoXzoy/aoMpLyTHeppWBCyb6pnjArR0TlZqEELUu00Ry1mdbwGTV3yqvwyEwkiWMMevXXi1kqYmGqCcYf3DEI7uotpWX7zW4wIx9esfgT+qX+bBk0ew8MFxlEolWZ/9P5eORfrdz25okLTo4C4+55QgNDq/u8D17Wr6NrWjC5967mht1ybZOhXD0jb+uPkSpaRLUMvHo6Uv2ULOz1CQmsFIyK3jt3NO9e6J4LtjeD4WkEVZf32qej3orbUQmRczqSLkZt3/y3LO7XZvI1RR1hW5okNwNO33EwRAJ6EL7oBYQ9++NZKwa2Ha8bND4bIEUAK6Tr6wL2wAVdQoGqkL119ZO0gvUC1eUoTVYN8LxHKmsFPzdUeqASF51H9VHwlH3eAA63Q55xMRTD1dt7bpcCRKzs5ymMMGu/Y9UPAFIQCZ4zW7/xKv2ZRHlp6T0pcpLfgPlcpNTUXZwXxHFwzwYhx2Uyq30dGOicFahTVuFdsOAlEa3OjVwZBX7dea6r7DH+suK515Cc/lUF/1MqmkIVRfAtBMaFsswGVHoHTEqy921ybx1ivw2k6KG4ZHqZp2/GRwU7OjiQhgksVhXGwtskng6SlxNoqZZcyXX6pUsHjW1SULXCGVBCwzUWUrjPlWcbPDhEPE2IQ+j54EEgcKuFxKI2RO6vuAIS2X6NGPOYSIV0DHp2fOFcQe9lyI9DlQxcGZfJiLevfnNnpPA+wvMKkrb/d9+fZxw7yrsZdvaN8SYZfzyIKO26nddQ7IWMXgYDWqvNLh3FMJrexj6pmUIBm+J3RhxQPGpjrhyy2Jm0rBDYjc8Cc4N1eGq6ILeZ00wwjJNiBGupS3wVLJvO32Ns1zaH9YAnINZpZeyD7UWpHGU0Jnfk\",\"associated_data\":\"payscore\",\"nonce\":\"syiC10wQR6PA\"}}";
        String publicKeyStr = "BhtczA3A4X6fvDDmOWN/qyyZSKq6DS/k/5EaApkjtJ78sl2HC2rQJGcMGoiO8HGd40vs+ockxwSmbXYEgoLBjJrsNRyX8zdnciw7zuI1kFz4YRteBJ9mGB9oHgSXihGRI88wvYiuiNEUfwXPZcEd/3W5NhTVtqEbXhq9K1ZjATSLaxsNy0mIeOcfY2VGfeW6v4UQmsF7EkqdICwfH2gnbD4tUj21ATmJi7ViY0E6VAsalts0Bdytv6bSPhruK9Ig3gkF6PAig6uyO/FEpwSQyWx34Upz8FHyamVBE/zI6IRGeNKgDPHz9meHkWUA1Ye3OAMq3EOLn69u6ZmJd8e6lQ==";

        boolean isValid = verifySignature(wechatpaySignature, timestamp, nonce, body, publicKeyStr);
        System.out.println("验签结果: " + isValid);
    }
}